﻿namespace MyTRCP.Common.MyProtocol
{
    public class LinkControlPayload : BitWalkableObj
    {
        public bool IsResponse { get; set; } //b15
        public LinkControlCmdEnum Cmd { get; set; }//b14:0
        public byte[] Data { get; set; } = [];
        public LinkControlPayload() { }
        public LinkControlPayload(bool isResponse, LinkControlCmdEnum cmd, byte[] data)
        {
            IsResponse = isResponse;
            Cmd = cmd;
            Data = data;
        }
        public LinkControlPayload(byte[] data) : base(data) { }

        public override BitWalker WriteToBitWalker(BitWalker bw)
        {
            return bw
                .WriteBit(IsResponse)
                .WriteBits((short)Cmd, 15)
                .WriteBytes(Data);
        }

        public override BitWalker ReadFromBitWalker(BitWalker bw)
        {
            IsResponse = bw.ReadBit();
            Cmd = (LinkControlCmdEnum)bw.ReadBits(15);
            Data = bw.ReadBytesToEnd();
            return bw;
        }
    }
}